package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingPlanServiceImpl nursingPlanService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {

        List<NursingLevel> nursingLevelList = list();
        // 转换
        List<NursingLevelVo> nursingLevelVos = nursingLevelList.stream().map(level -> {
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(level, nursingLevelVo);
            return nursingLevelVo;
        }).collect(Collectors.toList());
        // 关联
        nursingLevelVos.forEach(nursingLevelVo->{
            NursingPlan byId = nursingPlanService.getById(nursingLevelVo.getPlanId());
            if (byId != null) {
                nursingLevelVo.setPlanName(byId.getPlanName());
            }
        }
        );
        return nursingLevelVos;
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return updateById(nursingLevel) ? 1 : 0;
    }



    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        NursingLevel byId = getById(id);
        if (byId.getStatus()==1){
            throw new RuntimeException("该护理等级正在使用中，请勿删除");
        }
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<NursingLevel> selectAllNursingLevel() {
        List<NursingLevel>  list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (list != null) {
            log.info("缓存命中");
            return list;
        }
        list = list(new LambdaQueryWrapper<NursingLevel>().eq(NursingLevel::getStatus, 1));
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
